/**	
 * player的入口文件
 */
define(['./Audio','./RSLoader','./Layer/main','./Synchronizer','async','eventEmitter','./Util'],
	function(Audio,RSLoader,Layer,Synchronizer,async,EventEmitter,util){
		var Player = function(canvasId,audioId,rsUrl){
			//组件
			this.audio = new Audio();
			this.rsLoader = new RSLoader();
			this.layer = new Layer();
			this.sync = new Synchronizer(this.audio,this.layer,this.rsLoader);
			
			
			//常用标识
			/**
			 * Player状态
			 * notLoadData		未加载资源
			 * loadingData		正在加载资源
			 * loadDataFaild 	加载资源失败
			 * ready			加载资源成功并准备播放
			 * play				开始播放
			 * pause			暂停播放
			 * loading			加载数据中
			 */
			this.status = "notLoadData";
			
			//方法
			/**
			 * 初始化方法
			 */
			this.init = function(){
				//1.先加载初始化资源
				//2.同时载初始化音频
				//3.在加载完资源后载初始化图层
				async.auto({
					initRs: function (callback) {
						this.status = "loadingData";
			        	this.rsLoader.init(rsUrl,function(err){
			        		callback(err);
			        	});
			    	}.bind(this),
				    initAudio: function (callback) {
				        this.audio.init(audioId,rsUrl,function(){
				        	callback();
				        });
				        
				    }.bind(this),
				    initSync: function (callback) {
				        this.sync.init();
				        callback();
				    }.bind(this),
				    initLayer: ['initRs', function(callback) {
				        this.layer.init(canvasId,this.rsLoader.imgObj);
				        callback();
				    }.bind(this)],
				    //初始化事件
				    initEvent : ['initAudio',function(callback){
				    	this.audio.on('play',function(){
				    		this.emit('play');
				    		this.status = 'play';
				    	}.bind(this));
				    	
				    	this.audio.on('pause',function(){
				    		this.emit('pause');
				    		this.status = 'pause';
				    	}.bind(this));
				    	
				    	this.audio.on('seeked',function(){
				    		this.emit('seeked');
				    	}.bind(this));
				    	
				    	this.layer.on('loading',function(){
				    		this.audio.pause();
				    		this.status = 'loading';
				    	}.bind(this));
				    	
				    	this.layer.on('loadover',function(){
				    		if(this.status == "loading"){
				    			this.audio.play();
				    			this.status = 'play';
				    		}
				    			
				    	}.bind(this));
				    	callback();
				    }.bind(this)]
					}, function(err, results) {
						if(err){
							//加载资源失败
							this.status = "loadDataFaild";
							this.emit('loadRsError',err);
						}else{
							//加载资源成功
							this.status = "ready";
							this.emit('loadRsSuccess');
						}
						console.log('初始化完成');
					}.bind(this)
				);
			}
			
			/**
			 * 播放方法
			 */
			this.play = function(){
				if(this.status != "ready" && this.status != "pause"){
					return false;
				}
				this.audio.play();
			}
			
			/**
			 * 暂停方法
			 */
			this.pause = function(){
				this.audio.pause();
			}
			
			/**
			 * 返回audio是否正在播放
			 */
			this.isPlaying = function(){
				return this.audio.isPlaying();
			}
			
			/**
			 * 得到缓冲百分比
			 */
			this.getBufferedPercent = function(){
				return this.audio.getBufferedPercent();
			}
			
			/**
			 * 得到总时长
			 */
			this.getDuration = function(){
				return this.audio.getAudioDuration();
			}
			
			/**
			 * 得到当前播放时间
			 */
			this.getCurrentTime = function(){
				return this.audio.getCurrentTime();
			}
			
			/**
			 * 设置当前播放时间
			 * @param {Int} time 播放时间
			 */
			this.setCurrentTime = function(time){
				this.audio.setCurrentTime(time);
			}

			
			/**
			 * 得到当前播放时间百分比
			 */
			this.getCurrentTimePercent = function(){
				return this.audio.getCurrentTimePercent();
			}
			
			/**
			 * 设置音量大小
			 * @param {Float} volume 音量大小 0~1
			 */
			this.setVolume = function(volume){
				this.audio.setVolume(volume);
			}
			
			/**
			 * 得到音量大小
			 */
			this.getVolume = function(){
				return this.audio.getVolume();
			}
			
		};
		
		//继承事件
		util.inherits(Player,EventEmitter);
		return Player;
	}
);